home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / PalmGray / Src / grayEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-21  |  10.8 KB  |  424 lines  |  [TEXT/CWIE]

  1. /***********************************************************************
  2.  *
  3.  *    Copyright © Palm Computing 1998 -- All Rights Reserved
  4.  *
  5.  *
  6.  **********************************************************************/
  7.  
  8. #include <Pilot.h>                // all the system toolbox headers
  9. #include "GrayRsc.h"        // application resource defines
  10. #include "GrayEdit.h"
  11.  
  12. #define gBigBW     0
  13. #define gSmallBW   1
  14. #define gBigGray   2
  15. #define gSmallGray 3
  16.  
  17. // external globals we use
  18. extern Boolean gSupportsGrayAPIs;
  19. extern Boolean gHasGrayScreen;
  20.  
  21. // local globals
  22. BitmapPtr gBmps[4];
  23. int gCurrentlyEditing;
  24. int gCurrentDepth;
  25. int gPenColor;
  26.  
  27. typedef Byte * UBP;
  28.  
  29. /***********************************************************************/
  30. static void DrawARect(int x, int y, int wid, int hei, int color)
  31. {
  32.     static const RGBColorType black = {0x00, 0x00, 0x00, 0x00};
  33.     static const RGBColorType dkGray = {0x00, 0x55, 0x55, 0x55};
  34.     static const RGBColorType ltGray = {0x00, 0xAA, 0xAA, 0xAA};
  35.     static const RGBColorType white = {0x00, 0xFF, 0xFF, 0xFF};
  36.     static CustomPatternType dkGrayPat = {0xAAAA, 0x5555, 0xAAAA, 0x5555};
  37.     static CustomPatternType ltGrayPat = {0x8822, 0x8822, 0x8822, 0x8822};
  38.  
  39.     RGBColorType theColor, oldcolor;
  40.     RectangleType r;
  41.     CustomPatternType oldpattern, ppat;
  42.     FormPtr frm;
  43.     int i;
  44.     
  45.     r.topLeft.x = x;
  46.     r.topLeft.y = y;
  47.     r.extent.x = wid;
  48.     r.extent.y = hei;
  49.  
  50.     if (gSupportsGrayAPIs) {
  51.         switch (color) {
  52.             case 0: WinSetForeColor(&white,  &oldcolor); break;
  53.             case 1: WinSetForeColor(<Gray, &oldcolor); break;
  54.             case 2: WinSetForeColor(&dkGray, &oldcolor); break;
  55.             default: WinSetForeColor(&black, &oldcolor); break;
  56.         }
  57.         //WinSetForeColor(&theColor, &oldcolor);
  58.         //WinSetPattern(blackPattern);
  59.         //WinDrawRectangle(&r, 0);
  60.         for (i=0; i<hei; i++) {
  61.             WinDrawLine(r.topLeft.x, r.topLeft.y + i, r.topLeft.x + wid, r.topLeft.y + i);
  62.         }
  63.         WinSetForeColor(&oldcolor, &oldcolor);
  64.         WinSetForeColor(&black, &oldcolor); // this shouldn't have to be done but it is!
  65.     } else {
  66.         if (color==3)
  67.             WinDrawRectangle(&r, 0);
  68.         else if (color==0)
  69.             WinEraseRectangle(&r, 0);
  70.         else {
  71.             WinGetPattern(oldpattern);
  72.             if (color==2)
  73.                 WinSetPattern(dkGrayPat);
  74.             else
  75.                 WinSetPattern(ltGrayPat);
  76.             WinFillRectangle(&r, 0);
  77.             WinSetPattern(blackPattern);  // this should be oldPattern, I think, but that doesn't restore things right.
  78.         }
  79.     }
  80. }
  81.  
  82. /***********************************************************************/
  83. static void DrawPenPaint(int which, int color)
  84. {
  85.     RectangleType r;
  86.     FormPtr frm;
  87.     
  88.     frm = FrmGetActiveForm();
  89.     FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, which), &r);
  90.  
  91.     WinEraseRectangle(&r, 0);
  92.     DrawARect(r.topLeft.x+1, r.topLeft.y+1, r.extent.x -3, r.extent.y -2, color);
  93.  
  94.     //RctInsetRectangle(&r, -1);
  95.     if (color == gPenColor) {
  96.         WinDrawRectangleFrame(2, &r);
  97.     } else {
  98.         WinEraseRectangleFrame(2, &r);
  99.         WinDrawRectangleFrame(simpleFrame, &r);
  100.     }
  101. }
  102.  
  103. /***********************************************************************/
  104. void DrawPenPaints()
  105. {
  106.     DrawPenPaint(GrayMainPen0Gadget,0);
  107.     DrawPenPaint(GrayMainPen1Gadget,1);
  108.     DrawPenPaint(GrayMainPen2Gadget,2);
  109.     DrawPenPaint(GrayMainPen3Gadget,3);
  110.     
  111. }
  112.  
  113. /***********************************************************************/
  114. void DrawEditor()
  115. {
  116.     RectangleType r;
  117.     FormPtr frm;
  118.     
  119.     frm = FrmGetActiveForm();
  120.     FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, GrayMainPixelGadget), &r);
  121.     WinEraseRectangle(&r, 0);
  122.     RctInsetRectangle(&r, -1);
  123.     WinDrawRectangleFrame(simpleFrame, &r);
  124. }
  125.  
  126.  
  127. /***********************************************************************/
  128. static void DrawBitmapInGadget(int whichOne, Word obj)
  129. {
  130.     RectangleType r;
  131.     FormPtr frm;
  132.     BitmapPtr pBmp;
  133.     
  134.     frm = FrmGetActiveForm();
  135.     FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, obj), &r);
  136.     RctInsetRectangle(&r, -1);
  137.     WinDrawRectangleFrame(simpleFrame, &r);
  138.         
  139.     pBmp = gBmps[whichOne];
  140.     if (pBmp)
  141.         WinDrawBitmap(pBmp, r.topLeft.x, r.topLeft.y);
  142.     else {
  143.         RctInsetRectangle(&r, 1);
  144.         WinEraseRectangle(&r, 0);
  145.     }
  146. }
  147.  
  148.  
  149. /***********************************************************************/
  150. void DrawEditorAndDisplays()
  151. {
  152.     DrawEditor();
  153.     DrawPenPaints();
  154.     DrawBitmapInGadget(gBigBW,     GrayMainBigBWGadget);
  155.     DrawBitmapInGadget(gSmallBW,   GrayMainSmallBWGadget);
  156.     DrawBitmapInGadget(gBigGray,   GrayMainBigGrayGadget);
  157.     DrawBitmapInGadget(gSmallGray, GrayMainSmallGrayGadget);
  158. }
  159.  
  160. /***********************************************************************/
  161. static UBP FindBitsPtr(BitmapPtr bmp, int whichDepth)
  162. // if whichDepth is -1 it'll just use the first depth
  163. {
  164.     Word nextOffset; 
  165.     
  166.     if (bmp->pixelSize) {
  167.         do {
  168.             // thisDepth = (UBP) &(bmp->pixelSize);
  169.             //nextOffset = *((Word *) (thisDepth+2)); // hack to read a word out
  170.             if ( (bmp->pixelSize==whichDepth) || (whichDepth <0))
  171.                 return sizeof(BitmapType) + ((UBP) bmp);
  172.                 
  173.             nextOffset = bmp->nextDepthOffset;
  174.             bmp = (BitmapPtr) (nextOffset + ((UBP) bmp));
  175.         } while (nextOffset);
  176.         return 0; // we've reached the end of the structure and haven't found it; bail.
  177.     } else {
  178.         // old-style means it is a one-bit image
  179.         return (UBP) (++bmp);
  180.     }
  181. }
  182.  
  183.  
  184. /***********************************************************************/
  185. static int calcRowBytes(int w, int depth)
  186. {
  187.     int rowBytes;
  188.     int divisor;
  189.     
  190.     divisor = 8;
  191.     while (depth > 1) {
  192.         divisor = divisor >> 1;
  193.         depth--;
  194.     }
  195.     rowBytes = 1 + (w >> depth);  // / divisor
  196.     if (rowBytes & 0x01) 
  197.         rowBytes ++;  // it must be even so bump up
  198.         
  199.     return rowBytes;
  200. }
  201.  
  202. /***********************************************************************/
  203. static void SetPixel(Byte value, SWord x, SWord y, BitmapPtr bmp)
  204. // always works with the first depth in the image
  205. // supports up to 8 bits deep
  206. {
  207.     int col, depth, shift, mask, theByte, remainder;
  208.     UBP bits;
  209.     int byteOffset;
  210.     
  211.     // silently ignore bad param
  212.     if ((x > bmp->width) || (y > bmp->height))
  213.         return;
  214.     
  215.     bits = FindBitsPtr(bmp, -1);
  216.     if (bmp->pixelSize)
  217.         depth = bmp->pixelSize;
  218.     else
  219.         depth=1;
  220.     
  221.     switch (depth) {
  222.         case 0: 
  223.         case 1: shift=3; break;
  224.         case 2: shift=2; break;
  225.         case 4: shift=1; break;
  226.         case 8: shift=0; break;
  227.     }
  228.     
  229.     byteOffset = (bmp->rowBytes * y) + (x >> shift);
  230.     remainder = x - ((x >> shift) << shift);
  231.  
  232.     if (depth==1) {
  233.         value = value << 7; // put it in the highest position
  234.         mask  = 0x80;
  235.         while (remainder) {
  236.             mask = mask >> 1;
  237.             value=value >> 1;
  238.             remainder--;
  239.         }
  240.         mask = ~mask;
  241.     } else if (depth==2) {
  242.         value = value << 6;
  243.         mask  = 0xc0;
  244.         while (remainder) {
  245.             value = value >> 2;
  246.             mask  = mask  >> 2;
  247.             remainder--;
  248.         }
  249.         mask = ~mask;
  250.     } else if (depth==8) {
  251.         mask=0xff;
  252.     }
  253.  
  254.     bits[byteOffset] = value | (bits[byteOffset] & (mask));
  255. }
  256.  
  257. /***********************************************************************/
  258. static int GetPixel(SWord x, SWord y, BitmapPtr bmp)
  259. {
  260.     return 0;
  261. }
  262.  
  263.  
  264. /***********************************************************************/
  265. static void DrawBigPixel(int x, int y, int color)
  266. {
  267. /*    static const RGBColorType black = {0x00, 0x00, 0x00, 0x00};
  268.     static const RGBColorType dkGray = {0x00, 0x55, 0x55, 0x55};
  269.     static const RGBColorType ltGray = {0x00, 0xAA, 0xAA, 0xAA};
  270.     static const RGBColorType white = {0x00, 0xFF, 0xFF, 0xFF};
  271.     static CustomPatternType dkGrayPat = {0xAAAA, 0x5555, 0xAAAA, 0x5555};
  272.     static CustomPatternType ltGrayPat = {0x8822, 0x8822, 0x8822, 0x8822};
  273.  
  274.     RGBColorType theColor, oldcolor;
  275.     CustomPatternType oldpattern, ppat;
  276. */    FormPtr frm;
  277.     RectangleType r;
  278.     int i;
  279.     
  280.     frm = FrmGetActiveForm();
  281.     FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, GrayMainPixelGadget), &r);    
  282.  
  283.     DrawARect((x*4) + r.topLeft.x, (4*y) + r.topLeft.y, 4,4, color);
  284.  
  285.  
  286. /*    RctOffsetRectangle(&r, x<<2, y<<2);
  287.     r.extent.x = 4;
  288.     r.extent.y = 4;
  289.  
  290.     if (gSupportsGrayAPIs) {
  291.         switch (color) {
  292.             case 1: theColor = dkGray; break;
  293.             case 2: theColor = ltGray; break;
  294.             case 3: theColor = white; break;
  295.             default: theColor = black; break;
  296.         }
  297.         WinSetForeColor(&theColor, &oldcolor);
  298.         //WinSetPattern(blackPattern);
  299.         //WinDrawRectangle(&r, 0);
  300.         for (i=0; i<r.extent.
  301.         WinSetForeColor(&oldcolor, &oldcolor);
  302.     } else {
  303.         if (color==3)
  304.             WinDrawRectangle(&r, 0);
  305.         else if (color==0)
  306.             WinEraseRectangle(&r, 0);
  307.         else {
  308.             WinGetPattern(oldpattern);
  309.             if (color==2)
  310.                 WinSetPattern(dkGrayPat);
  311.             else
  312.                 WinSetPattern(ltGrayPat);
  313.             WinFillRectangle(&r, 0);
  314.             WinSetPattern(blackPattern);  // this should be oldPattern, I think, but that doesn't restore things right.
  315.         }
  316.     }
  317. */
  318. }
  319.  
  320. /***********************************************************************/
  321. void HitPixel(SWord x, SWord y)
  322. {
  323.     FormPtr frm;
  324.     RectangleType r;
  325.     BitmapPtr bmp;
  326.     
  327.     frm = FrmGetActiveForm();
  328.     FrmGetObjectBounds (frm, FrmGetObjectIndex (frm, GrayMainPixelGadget), &r);
  329.         
  330.     // convert from screen pixels to the appropriate fatbit pixel
  331.     x = (x-r.topLeft.x) >> 2;
  332.     y = (y-r.topLeft.y) >> 2;
  333.     
  334.     bmp = gBmps[gCurrentlyEditing];
  335.     
  336.     // silently ignore bad click location
  337.     if ((x > bmp->width) || (y > bmp->height))
  338.         return;
  339.  
  340.     SetPixel(gPenColor, x, y, bmp);
  341.     
  342.     DrawBigPixel(x, y, gPenColor);
  343.     
  344.     //inefficient...
  345.     DrawBitmapInGadget(gBigBW,     GrayMainBigBWGadget);
  346.     DrawBitmapInGadget(gSmallBW,   GrayMainSmallBWGadget);
  347.     DrawBitmapInGadget(gBigGray,   GrayMainBigGrayGadget);
  348.     DrawBitmapInGadget(gSmallGray, GrayMainSmallGrayGadget);
  349. }
  350.  
  351.  
  352. /***********************************************************************/
  353. void SaveAndSwitch(Word switchTo)
  354. {
  355.     FormPtr frm;
  356.     int newOne;
  357.     
  358.     switch (switchTo) {
  359.         case GrayMainBigBWGadget:     newOne = gBigBW; break;
  360.         case GrayMainSmallBWGadget:   newOne = gSmallBW; break;
  361.         case GrayMainBigGrayGadget:   newOne = gBigGray; break;
  362.         case GrayMainSmallGrayGadget: newOne = gSmallGray; break;
  363.     }
  364.     
  365.     if (gCurrentlyEditing != newOne)
  366.         gCurrentlyEditing=newOne;
  367.     if ((newOne==gBigBW) || (newOne==gSmallBW))
  368.         gCurrentDepth=1;
  369.     else
  370.         gCurrentDepth=2;
  371.     
  372.     DrawEditorAndDisplays();
  373. }
  374.  
  375. /***********************************************************************/
  376. static BitmapPtr AllocateBmp(int w, int h, int depth)
  377. {
  378.     unsigned short rowBytes;  // must be even.
  379.     BitmapPtr bmp;
  380.     int bmpSize;
  381.     
  382.     rowBytes = calcRowBytes(w, depth);
  383.     
  384.     bmpSize = sizeof(BitmapType) + sizeof(ColorTableType) + (rowBytes * h * depth);
  385.     bmp = (BitmapPtr) MemPtrNew(bmpSize);
  386.     MemSet(bmp, bmpSize, 0);  // guarantee everything is cleared out
  387.     
  388.     if (bmp) {
  389.         bmp->width = w;
  390.         bmp->height = h;
  391.         bmp->rowBytes = rowBytes;
  392.         if (depth>1) {
  393.             // make a 3.0-style bitmap
  394.             bmp->pixelSize = depth;
  395.             bmp->version = 1;
  396.             bmp->nextDepthOffset = 0;  // this only has a single image in it
  397.             
  398.         } else {
  399.             // make an old-style bitmap object
  400.             // ... everything must be set to 0, which is done above.
  401.         }
  402.     }
  403.     
  404.     return bmp;
  405. }
  406.  
  407. /***********************************************************************/
  408. void SetPenTo(int value)
  409. {
  410.     gPenColor=value;
  411.     DrawPenPaints();
  412. }
  413.  
  414. /***********************************************************************/
  415. void InitGrayEdit()
  416. {
  417.     gCurrentlyEditing=gBigBW;
  418.     gPenColor=3;        // in B&W setPixel will do ok with this
  419.     gBmps[gBigBW]     = AllocateBmp(29,29,1); // 29x29 one bit image
  420.     gBmps[gSmallBW]   = AllocateBmp(15,9, 1);
  421.     gBmps[gBigGray]   = AllocateBmp(29,29,2);
  422.     gBmps[gSmallGray] = AllocateBmp(15,9, 2);
  423. }
  424.